import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取TIFF图像
img = cv2.imread("car.jpg", cv2.IMREAD_GRAYSCALE)

# 计算直方图
hists = cv2.calcHist([img], [0], None, [256], [0, 256])

# 计算累积分布函数（CDF）
hists_cumsum = np.cumsum(hists)

# 计算映射函数
level = 256  # 灰度级的最大值
m, n = img.shape
const_a = level / (m * n)
hists_cdf = (const_a * hists_cumsum).astype("uint8")

# 映射原图像
img_eq = hists_cdf[img]

# 使用matplotlib显示原图像和均衡化后的图像
plt.subplot(1, 2, 1)
plt.imshow(img, cmap="gray")
plt.title("Original Image")

plt.subplot(1, 2, 2)
plt.imshow(img_eq, cmap="gray")
plt.title("Equalized Image")

plt.show()

